<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Vue," />










<meta name="description" content="vue存储会话状态和页面传值 1.兄弟组件之间的传值 组件A向组件B传值： 我做的是登录界面login.vue向主页界面index.vue传值，登录界面在成功之后，会从后端拿到一个userId的值，由于vue页面生命周期的原因，该userId会在页面生命周期结束之后销毁，在另外一个页面无法引用该值，那么如果我想继续使用这个值，唯一的办法就是在销毁之前，先把这个值传递出去。  A组件(login.v">
<meta property="og:type" content="article">
<meta property="og:title" content="vue页面传值和会话存储">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="vue存储会话状态和页面传值 1.兄弟组件之间的传值 组件A向组件B传值： 我做的是登录界面login.vue向主页界面index.vue传值，登录界面在成功之后，会从后端拿到一个userId的值，由于vue页面生命周期的原因，该userId会在页面生命周期结束之后销毁，在另外一个页面无法引用该值，那么如果我想继续使用这个值，唯一的办法就是在销毁之前，先把这个值传递出去。  A组件(login.v">
<meta property="og:locale">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/login.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/index.png">
<meta property="article:published_time" content="2021-11-05T15:10:37.000Z">
<meta property="article:modified_time" content="2022-03-10T13:00:21.920Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://louchaoyy.gitee.io/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/login.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2021/11/05/vue页面传值和会话存储/"/>





  <title>vue页面传值和会话存储 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">vue页面传值和会话存储</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-11-05T23:10:37+08:00">
                2021-11-05
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2021/11/05/vue页面传值和会话存储/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id><a href="#" class="headerlink" title></a><blockquote class="blockquote-center"><p>vue存储会话状态和页面传值</p>
</blockquote></h1><h2 id="1-兄弟组件之间的传值"><a href="#1-兄弟组件之间的传值" class="headerlink" title="1.兄弟组件之间的传值"></a>1.兄弟组件之间的传值</h2><ul>
<li><p>组件A向组件B传值：</p>
<p>我做的是登录界面login.vue向主页界面index.vue传值，登录界面在成功之后，会从后端拿到一个userId的值，由于vue页面生命周期的原因，该userId会在页面生命周期结束之后销毁，在另外一个页面无法引用该值，那么如果我想继续使用这个值，唯一的办法就是在销毁之前，先把这个值传递出去。</p>
</li>
<li><p>A组件(<code>login.vue</code>)</p>
<span id="more"></span></li>
<li><p><strong>userId很重要，在以后的兄弟组件之间会用到很多</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//首先引用该bus.js文件</span></span><br><span class="line"><span class="keyword">import</span> bus <span class="keyword">from</span> <span class="string">&#x27;./bus&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">userId</span>:<span class="string">&#x27;&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>:&#123;</span><br><span class="line">        <span class="comment">//登录</span></span><br><span class="line">        <span class="keyword">async</span> <span class="function"><span class="title">login</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="built_in">this</span>.form.username==<span class="string">&#x27;&#x27;</span>||<span class="built_in">this</span>.form.password==<span class="string">&#x27;&#x27;</span>)&#123;</span><br><span class="line">                <span class="built_in">this</span>.$message.error(<span class="string">&quot;请输入账号或密码进行登录&quot;</span>)</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                <span class="comment">//打印表格中的内容看看</span></span><br><span class="line">                <span class="comment">// console.log(this.form.username+&quot; //&quot;+this.form.password)</span></span><br><span class="line">                <span class="built_in">this</span>.$router.push(<span class="string">&#x27;index&#x27;</span>)</span><br><span class="line">                <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(<span class="string">&#x27;/login&#x27;</span>,&#123;<span class="attr">params</span>:<span class="built_in">this</span>.form&#125;)</span><br><span class="line">                <span class="built_in">console</span>.log(res.data)</span><br><span class="line">                <span class="keyword">if</span>(res.data.status!=<span class="number">200</span>)&#123;</span><br><span class="line">                    <span class="built_in">this</span>.$message.error(res.data.meta)</span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    <span class="comment">//登录成功，清除登录框内的内容</span></span><br><span class="line">                    <span class="built_in">this</span>.form.username = <span class="string">&#x27;&#x27;</span></span><br><span class="line">                    <span class="built_in">this</span>.form.password = <span class="string">&#x27;&#x27;</span></span><br><span class="line">                    <span class="comment">//同时提示登录成功</span></span><br><span class="line">                    <span class="built_in">this</span>.$message.success(<span class="string">&quot;登录成功！！&quot;</span>)</span><br><span class="line">                    <span class="built_in">this</span>.userId = res.data.user_id</span><br><span class="line">                    <span class="comment">//向index页面传递，把userId传递过去</span></span><br><span class="line">                    bus.$emit(<span class="string">&#x27;login&#x27;</span>,<span class="built_in">this</span>.userId)</span><br><span class="line">                    <span class="comment">//跳转到welcome页面</span></span><br><span class="line">                    <span class="built_in">this</span>.$router.push(<span class="string">&#x27;index&#x27;</span>)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这里使用了事件中心来当一个中转站来进行中转，</p>
<p>逻辑处理时，A组件向事件中心提交一个事件，并且把需要传递的值传递出去</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bus.$emit(<span class="string">&#x27;login&#x27;</span>,<span class="built_in">this</span>.userId)</span><br></pre></td></tr></table></figure>

<p>事件中心在这里用在B组件中的<strong>mounted</strong>钩子进行监听</p>
</li>
<li><p>B组件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//首先引用该bus.js文件</span></span><br><span class="line"><span class="keyword">import</span> bus <span class="keyword">from</span> <span class="string">&#x27;./bus&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            <span class="attr">userId</span>:<span class="string">&#x27;&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>:&#123;</span><br><span class="line">        <span class="comment">//发表文章</span></span><br><span class="line">        <span class="function"><span class="title">submit</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            <span class="comment">//关闭对话框</span></span><br><span class="line">            <span class="built_in">this</span>.addDialogVisible = <span class="literal">false</span></span><br><span class="line">            <span class="comment">//给内容赋值</span></span><br><span class="line">            <span class="comment">//截取前30个字符</span></span><br><span class="line">            <span class="built_in">this</span>.article_form.arr = <span class="built_in">this</span>.article_form.content.substr(<span class="number">0</span>,<span class="number">150</span>)</span><br><span class="line"></span><br><span class="line">            <span class="comment">//给文章发表的时间赋值</span></span><br><span class="line">            <span class="built_in">this</span>.article_form.time = <span class="built_in">this</span>.getTime()</span><br><span class="line">            <span class="comment">//弹出提示</span></span><br><span class="line">            <span class="built_in">this</span>.$message.success(<span class="string">&quot;发表成功&quot;</span>)</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&quot;来自兄弟组件的userId:&quot;</span>+<span class="built_in">this</span>.userId)</span><br><span class="line">            </span><br><span class="line">        &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">// 监听兄弟组件传过来的值</span></span><br><span class="line">        bus.$on(<span class="string">&#x27;login&#x27;</span>,<span class="function">(<span class="params">userid</span>)=&gt;</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&quot;来自login组件&quot;</span>+userid)</span><br><span class="line">            <span class="built_in">this</span>.userId = userid</span><br><span class="line">            <span class="comment">//但是这个值在回调函数里面</span></span><br><span class="line">        &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样，就拿到了兄弟组件传递过来的值，<strong>弊端：回调函数里面的值不好取出来，需要多次回调函数，逻辑复杂，理解起来困难</strong></p>
</li>
</ul>
<h2 id="2-使用页面间的传值"><a href="#2-使用页面间的传值" class="headerlink" title="2.使用页面间的传值"></a>2.使用页面间的传值</h2><ul>
<li><p>在A页面中</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">this</span>.$router.push(&#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;routePage&#x27;</span>,</span><br><span class="line">    query/params: &#123;</span><br><span class="line">   		 <span class="attr">routeParams</span>: params</span><br><span class="line">	&#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
<li><p>在B页面中的<strong>mounted钩子</strong>或者<strong>created钩子</strong>中接收值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">created</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">     <span class="keyword">var</span> name = <span class="built_in">this</span>.$route.params.routeParams</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>缺点：由于使用<code>params</code>传参数时，页面刷新，参数会消失，而使用<code>query</code>传递参数虽然页面刷新，参数不消失，但是通过url传递值，这样会导致<code>url</code>过长，如果保存重要信息，直接显示在<code>url</code>中，很不安全</strong></p>
</li>
</ul>
<h2 id="3-最安全最好的（localStorage）"><a href="#3-最安全最好的（localStorage）" class="headerlink" title="3.最安全最好的（localStorage）"></a>3.最安全最好的（<strong>localStorage</strong>）</h2><ul>
<li><p>利用<strong>localStorage</strong>可以保存用户的登录状态，类似于<strong>seeion</strong>，保存在浏览器中，5M的空间能够存上很多，也已经足够使用</p>
</li>
<li><p>在A页面或者有需求的存储登录和会话状态的页面，设置好localStorage</p>
<p>我这里的业务逻辑是，登录成功之后，把后端返回到前端的<strong>user_id</strong>保存下来，前端<code>login.vue</code>传递到<code>index.vue</code>或者更多的页面</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//登录</span></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="title">login</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.form.username==<span class="string">&#x27;&#x27;</span>||<span class="built_in">this</span>.form.password==<span class="string">&#x27;&#x27;</span>)&#123;</span><br><span class="line">        <span class="built_in">this</span>.$message.error(<span class="string">&quot;请输入账号或密码进行登录&quot;</span>)</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="comment">//打印表格中的内容看看</span></span><br><span class="line">        <span class="comment">// console.log(this.form.username+&quot; //&quot;+this.form.password)</span></span><br><span class="line">        <span class="comment">// this.$router.push(&#x27;index&#x27;)</span></span><br><span class="line">        <span class="keyword">const</span> res = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(<span class="string">&#x27;/login&#x27;</span>,&#123;<span class="attr">params</span>:<span class="built_in">this</span>.form&#125;)</span><br><span class="line">        <span class="built_in">console</span>.log(res.data)</span><br><span class="line">        <span class="keyword">if</span>(res.data.status!=<span class="number">200</span>)&#123;</span><br><span class="line">            <span class="built_in">this</span>.$message.error(res.data.meta)</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="comment">//登录成功，清除登录框内的内容</span></span><br><span class="line">            <span class="built_in">this</span>.form.username = <span class="string">&#x27;&#x27;</span></span><br><span class="line">            <span class="built_in">this</span>.form.password = <span class="string">&#x27;&#x27;</span></span><br><span class="line">            <span class="comment">//同时提示登录成功</span></span><br><span class="line">            <span class="built_in">this</span>.$message.success(<span class="string">&quot;登录成功！！&quot;</span>)</span><br><span class="line">            <span class="built_in">this</span>.userId = res.data.user_id</span><br><span class="line">            <span class="comment">//跳转到welcome页面</span></span><br><span class="line">            <span class="built_in">this</span>.$router.push(<span class="string">&#x27;index&#x27;</span>)</span><br><span class="line">            <span class="comment">//用localStorage存储登录状态</span></span><br><span class="line">            <span class="built_in">localStorage</span>.setItem(<span class="string">&#x27;userId&#x27;</span>,<span class="built_in">this</span>.userId)<span class="comment">//============保存状态==============//</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure></li>
<li><p>在需要用到的页面，直接取出来即可</p>
<p>这里我放到了<strong>created钩子</strong>里面，当然也可以放到<strong>mounted钩子</strong>，甚至放到<strong>methods</strong>方法里面，亲测试，完全没问题</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">created</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//把登录成功存储的localStorage里面的userId拿出来，赋值给本组件的userId</span></span><br><span class="line">    <span class="built_in">this</span>.userId = <span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;userId&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>结果如下图</p>
<img src="/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/login.png" class title="login登录情况">

<img src="/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/index.png" class title="userId存储情况"></li>
</ul>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/Vue/" rel="tag"># Vue</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2021/11/04/node-js%E5%AE%9E%E7%8E%B0%E5%90%8E%E7%AB%AF%E7%99%BB%E5%BD%95%E6%B3%A8%E5%86%8C%E6%8E%A5%E5%8F%A3/" rel="next" title="node.js实现后端登录注册接口">
                <i class="fa fa-chevron-left"></i> node.js实现后端登录注册接口
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2022/03/10/JavaScript%E4%B8%AD%E5%AF%B9%E9%97%AD%E5%8C%85%E7%9A%84%E7%90%86%E8%A7%A3/" rel="prev" title="JavaScript中对闭包的理解">
                JavaScript中对闭包的理解 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">1.</span> <span class="nav-text">vue存储会话状态和页面传值
</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%85%84%E5%BC%9F%E7%BB%84%E4%BB%B6%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BC%A0%E5%80%BC"><span class="nav-number">1.1.</span> <span class="nav-text">1.兄弟组件之间的传值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8%E9%A1%B5%E9%9D%A2%E9%97%B4%E7%9A%84%E4%BC%A0%E5%80%BC"><span class="nav-number">1.2.</span> <span class="nav-text">2.使用页面间的传值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%9C%80%E5%AE%89%E5%85%A8%E6%9C%80%E5%A5%BD%E7%9A%84%EF%BC%88localStorage%EF%BC%89"><span class="nav-number">1.3.</span> <span class="nav-text">3.最安全最好的（localStorage）</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2021/11/05/vue%E9%A1%B5%E9%9D%A2%E4%BC%A0%E5%80%BC%E5%92%8C%E4%BC%9A%E8%AF%9D%E5%AD%98%E5%82%A8/';
          this.page.identifier = '2021/11/05/vue页面传值和会话存储/';
          this.page.title = 'vue页面传值和会话存储';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
